package com.yc.sole.erp.controller;

import com.yc.sole.common.domain.StateCountResp;
import com.yc.sole.erp.biz.SaleOrderBiz;
import com.yc.sole.erp.request.SaleOrderQueryReq;
import com.yc.sole.erp.request.SaleOrderReq;
import com.yc.sole.erp.request.SaleOrderSaveReq;
import com.yc.sole.erp.response.SaleOrderBomResp;
import com.yc.sole.erp.response.SaleOrderResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 销售订单
 *
 * @author yizuomin
 * @date 2023-12-15
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class SaleOrderController {

    private final SaleOrderBiz saleOrderBiz;

    /**
     * 新增
     *
     * @param req
     * @return RestResult
     */
    @PreAuth(value = "sale:order:save", description = "新增销售订单")
    @PostMapping("/api/sale/order/save")
    public Result<List<Long>> saveSaleOrder(@RequestBody @Validated(SaveGroup.class) SaleOrderSaveReq req) {
        return Result.data(saleOrderBiz.saveSaleOrder(req));
    }

    /**
     * 通过Id修改
     *
     * @param saleOrderReq
     * @return RestResult
     */
    @PreAuth(value = "sale:order:update", description = "修改销售订单")
    @PostMapping("/api/sale/order/update")
    public Result<Void> updateSaleOrderById(@RequestBody @Validated(UpdateGroup.class) SaleOrderReq saleOrderReq) {
        saleOrderBiz.updateSaleOrderById(saleOrderReq);
        return Result.success();
    }

    /**
     * 取消订单
     *
     * @param orderId
     * @return
     */
    @PreAuth(value = "sale:order:update", description = "取消订单")
    @PostMapping("/api/sale/order/cancel/{orderId}")
    public Result<Void> cancelOrder(@PathVariable Long orderId) {
        saleOrderBiz.cancelOrder(orderId);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "sale:order:delete", description = "删除销售订单")
    @PostMapping("/api/sale/order/delete")
    public Result<Void> deleteSaleOrder(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        saleOrderBiz.deleteSaleOrder(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "sale:order:select", description = "查询销售订单")
    @PostMapping("/api/sale/order/page")
    public Result<Paging<SaleOrderResp>> selectSaleOrderPage(@RequestBody @Validated BasePageReq<SaleOrderQueryReq> basePageReq) {
        return Result.data(saleOrderBiz.selectSaleOrderPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 根据订单id查询订单详情
     *
     * @param id
     * @return RestResult
     */
    @PreAuth(value = "sale:order:select", description = "根据订单id查询销售订单详情")
    @PostMapping("/api/sale/order/{id}")
    public Result<SaleOrderResp> selectSaleOrderById(@PathVariable Long id) {
        return Result.data(saleOrderBiz.selectSaleOrderById(id));
    }

    /**
     * 根据订单编号查询订单详情
     *
     * @param saleOrderQueryReq
     * @return RestResult
     */
    @PreAuth(value = "sale:order:select", description = "根据订单编号查询订单详情")
    @PostMapping("/api/sale/order/list")
    public Result<List<SaleOrderResp>> list(@RequestBody SaleOrderQueryReq saleOrderQueryReq) {
        return Result.data(saleOrderBiz.selectSaleOrderByOrderNoLike(saleOrderQueryReq.getOrderNo()));
    }

    /**
     * 利润计算
     *
     * @param orderId
     * @return RestResult
     */
    @PreAuth(value = "sale:order:select", description = "利润计算")
    @PostMapping("/api/sale/order/profit-calculation/{orderId}")
    public Result<List<SaleOrderBomResp>> profitCalculation(@PathVariable Long orderId) {
        return Result.data(saleOrderBiz.profitCalculation(orderId));
    }

    /**
     * 导出订单
     *
     * @param queryReq
     * @return RestResult
     */
    @PreAuth(value = "sale:order:select", description = "导出订单")
    @PostMapping("/api/sale/order/export")
    public void export(@RequestBody SaleOrderQueryReq queryReq, HttpServletResponse response) {
        saleOrderBiz.export(queryReq, response);
    }

    /**
     * 查询订单状态的数量
     *
     * @return RestResult
     */
    @PreAuth(value = "sale:order:select", description = "查询订单状态的数量")
    @PostMapping("/api/sale/order/order-state-count")
    public Result<List<StateCountResp>> selectOrderStateCount(@RequestBody SaleOrderQueryReq queryReq) {
        return Result.data(saleOrderBiz.selectOrderStateCount(queryReq));
    }

    /**
     * 提前终止订单
     *
     * @return RestResult
     */
    @PreAuth(value = "sale:order:update", description = "提前终止订单")
    @PostMapping("/api/sale/order/termination")
    public Result<Void> termination(@RequestBody @Validated(UpdateGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        saleOrderBiz.termination(baseBatchIdsReq.getIds());
        return Result.success();
    }
}
